//
//  OrderViewController.m
//  YJlandlord
//
//  Created by 文彬 on 2018/6/24.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "OrderViewController.h"
#import "OrderInfoTableViewCell.h"
#import "OrderTimeTableViewCell.h"
#import "OrderOperationTableViewCell.h"
#import "WBScrollSelectView.h"
#import "OrderDetailViewController.h"
#import "OrderTableViewCell.h"

@interface OrderViewController ()<UITableViewDataSource,
    UITableViewDelegate,
    WBScrollSelectDelegate>

@property (strong, nonatomic) UITableView *listTableView;
@property (strong, nonatomic) WBScrollSelectView *scrllSelectView;

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"订单";
    
    [self initPageControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark 页面相关
- (void)initPageControl{
    
    [self.view addSubview:self.scrllSelectView];
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@(self.scrllSelectView.frame.size.height+1));
        make.bottom.equalTo(@0);
    }];
    
}


#pragma mark WBScrollSelectDelegate
- (void)ScrollSelectDidCickWith:(int)num{
    
}


#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 45;
    }
    else if(indexPath.row==1)
    {
        return 150;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        
        OrderTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTimeTableViewCell" forIndexPath:indexPath];
        return cell;
    }
    else if(indexPath.row==1){
        
        OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTableViewCell" forIndexPath:indexPath];
        cell.imgView.layer.cornerRadius = 5;
        cell.imgView.clipsToBounds = YES;
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
        cell.dateLabel.text = @"2018年11月11日--2018年11月12日";
        cell.timeLabel.text = @"09:00--11:00";
        if (indexPath.section%2==0) {
            [cell showTimeLabel:YES];
        }
        else{
            [cell showTimeLabel:NO];
        }
        
        return cell;
    }
   
    static NSString *iden = @"iden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = kLineColor;
    [cell.contentView addSubview:lineView];
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@0.5);
        make.centerX.equalTo(cell.contentView.centerX);
        make.top.equalTo(@5);
        make.bottom.equalTo(@-5);
    }];
    
    float dis = (SCREEN_WIDTH/2-80)/2;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"重新下单" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.layer.cornerRadius = 4;
    button.backgroundColor = RGBCOLOR(202, 53, 56);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cell.contentView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-dis));
        make.width.equalTo(@80);
        make.height.equalTo(@35);
        make.centerY.equalTo(cell.contentView.centerY);
    }];
    
    UIButton *delebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [delebutton setTitle:@"删除订单" forState:UIControlStateNormal];
    delebutton.titleLabel.font = [UIFont systemFontOfSize:15];
    delebutton.layer.cornerRadius = 4;
    [delebutton setTitleColor:kTitleColor forState:UIControlStateNormal];
    delebutton.layer.borderColor  = kSecTitleColor.CGColor;
    delebutton.layer.borderWidth = 0.5;
    [cell.contentView addSubview:delebutton];
    [delebutton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(dis));
        make.width.equalTo(@80);
        make.height.equalTo(@35);
        make.centerY.equalTo(cell.contentView.centerY);
    }];
    
    
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    OrderDetailViewController *detailController = [[OrderDetailViewController alloc]init];
    detailController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailController animated:YES];
}
#pragma mark getter
- (WBScrollSelectView*)scrllSelectView{
    if (_scrllSelectView==nil) {
        _scrllSelectView = [[WBScrollSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) titles:@[@"全部",@"待付款",@"待入住",@"待评价"]];
        _scrllSelectView.delegate = self;
        _scrllSelectView.LineWith = 60;
    }
    return _scrllSelectView;
}

- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorColor = kLineColor;
        _listTableView.backgroundColor = [UIColor clearColor];
        [_listTableView registerNib:[UINib nibWithNibName:@"OrderTableViewCell" bundle:nil] forCellReuseIdentifier:@"OrderTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"OrderTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"OrderTimeTableViewCell"];
        
    }
    
    return _listTableView;
}


@end
